// pages/module/rebate/rebate.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import time from '../../../utils/time.js';
import {
  getData,
  setData,
  clearData,
  dataKey
} from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  checkXpOrder,
  retXpOrder,
  cancelXpOrder,
  orderPay,
  orderUpdate,
  orderConfirm
} from '../../../utils/imapi.js';
import Dialog from '../../common/components/vant/dialog/dialog';
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    sliderLeft: 55,
    sliderOffset: 150,
    is_screen: 0,
    height: app.globalData.screenHeight,
    tabs: [{
        "id": '',
        "name": "全部",
        "is_selected": 1
      },
      {
        "id": 0,
        "name": "待付款",
        "is_selected": 1
      },
      {
        "id": 1,
        "name": "待发货",
        "is_selected": 1
      },
      {
        "id": 2,
        "name": "待收货",
        "is_selected": 1
      },
      {
        "id": 3,
        "name": "已完成",
        "is_selected": 1
      },
    ],
    order_list: [],
    selectList: [{
        name: '价格有点贵',
        is_select: 1
      },
      {
        name: '规格/款式/数量拍错',
        is_select: 0
      },
      {
        name: '收货地址拍错',
        is_select: 0
      },
      {
        name: '暂时不需要了',
        is_select: 0
      },
      {
        name: '其他',
        is_select: 0
      }
    ],
    cancelId: '',
    img_list: "img_list.img_list"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      img_list: img_list.img_list
    })
    
  },
  async getOrderList(){
    let order_status = this.data.tabs[this.data.tabIndex].id
    const {
      data
    } = await checkXpOrder({
      order_status: order_status,
    })
    this.setData({
      order_list: data
    })
  },
  async onTabClick(e) {
    var index = e.detail.index;
    this.setData({
      tabIndex:index
    })
    await this.getOrderList( this.data.tabs[index].id);
  },
  cancle(e) {
    console.log(e);
    this.setData({
      show_part: true,
      cancelId: e.detail.index
    })
  },

  hideShare() {
    this.setData({
      show_part: false,
    })
  },
  select(e) {
    var index = e.currentTarget.dataset.index;
    this.data.selectList.forEach(item => {
      item.is_select = 0
    })
    this.data.selectList[index].is_select = 1
    this.setData({
      selectList: this.data.selectList
    })
  },
  //取消订单
  confirm() {
    let self = this;
    Dialog.confirm({
        title: '提示',
        message: '是否确认取消订单',
      })
      .then(() => {
        console.log(self.data.cancelId);
        let reason_info = self.data.selectList.filter(item=>item.is_select == 1)[0]
        cancelXpOrder({
          order_no: self.data.cancelId,
          reason_info: reason_info.name
        }).then(res => {
        })
      })
      .catch(() => {
      
      });

  },
  //支付
  async pay(e) {
    let self = this;
    let index =e.detail.index;
    let orderInfo = this.data.order_list[index];
    const data = {
      price:orderInfo.act_price*orderInfo.goods_amt,
      in_out: 'I',
      fin_type: 'GW',
      ref_order_no: orderInfo.order_id.toString(),
    }
    const res = await orderPay(data);
    if (res.code === 0) {
      wx.requestPayment({
        timeStamp: res.data.timestamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: res.data.signType,
        paySign: res.data.paySign,
        success: () => {
          orderUpdate({ 
            order_id:orderInfo.order_id.toString()
          }).then(res=>{
            self.getOrderList();
          })
        },
        fail: (err) => {
          console.log('失败', err)
        }
      })
    }
  },
  confirmOrder(e){
    let self = this;
    let index =e.detail.index;
    let orderInfo = this.data.order_list[index];
    Dialog.confirm({
      title: '提示',
      message: '是否确认收货',
    })
    .then(() => {
      orderConfirm({
        order_id:orderInfo.order_id.toString()
      }).then(res=>{
        self.getOrderList();
      })
    })
    .catch(() => {
    
    });
    
  },
  hideScreen() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrderList('');
    shareFun.hidenShareMenu();
     //app.globalData.uid
    var timestamp = Date.parse(new Date());
    var today = time.dateformat(timestamp)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})